<style></style>

<template>
  <div>
    <el-dialog
      title="设置报警阈值"
      :visible.sync="cycleVis"
      :destroy-on-close="true"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :before-close="cancelCycleVis"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="全程衰耗" prop="fullAttenThreshold">
          <el-input
            v-model="form.fullAttenThreshold"
            placeholder="请输入全程衰耗"
          >
            <template slot="append">db</template>
          </el-input>
        </el-form-item>
        <el-form-item label="全长阈值" prop="fullLengthThreshold">
          <el-input
            v-model="form.fullLengthThreshold"
            placeholder="请输入全长阈值"
          >
            <template slot="append">M</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancelCycleVis">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getThreshold, addThreshold, putThreshold } from "@/api/base/circuitThreshold";

export default {
  name: "settingThreshold",
  props: {
    cycleVis: {
      type: Boolean,
      default: false,
    },
    cycleObj: {
      type: Object,
      default: () => {
        return {};
      },
    },
    name: {
      type: String,
      default: "",
    },
  },
  watch: {
    cycleVis: {
      handler(val) {
        if (val) {
          this.getThreshold();
        }
      },
    },
  },
  data() {
    return {
      form: { fullAttenThreshold: 0, fullLengthThreshold: 0 },
      // 表单校验
      rules: {
        fullAttenThreshold: [
          { required: true, message: "全程衰耗不能为空", trigger: "blur" },
        ],
        fullLengthThreshold: [
          { required: true, message: "全长阈值不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getThreshold() {
      //查询阈值
      getThreshold(this.cycleObj.deviceNo, this.cycleObj.channel).then(
        (res) => {
          if (res.data) {
            this.form = {
              fullAttenThreshold: res.data.fullAttenThreshold,
              fullLengthThreshold: res.data.fullLengthThreshold,
              thresholdId: res.data.thresholdId,
              deviceNo: this.cycleObj.deviceNo,
              channel: this.cycleObj.channel,
            };
          } else {
            this.form = {
              fullAttenThreshold: 0,
              fullLengthThreshold: 0,
              thresholdId: null,
              deviceNo: this.cycleObj.deviceNo,
              channel: this.cycleObj.channel,
            };
          }
        }
      );
    },
    submitForm() {
      //修改阈值提交
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.thresholdId) {
            //是修改
            putThreshold(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.cancelCycleVis();
              if (this.name !== "equiList") {
                this.$parent.getList();
              }
            });
          } else {
            addThreshold(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.cancelCycleVis();
              if (this.name !== "equiList") {
                this.$parent.getList();
              }
            });
          }
        }
      });
    },
    cancelCycleVis() {
      this.$parent.cancelCycleVis();
    },
  },
};
</script>
